/**
 * 
 */
package co.com.umobile.dao;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import co.com.umobile.domain.Place;
import co.com.umobile.persistence.DatabaseManager;


/**
 * @since 26/07/2012
 */
public class PlaceDao implements GenericDao {
	
	/**
	   * gets the list places.
	   * 
	   * @param placeCode placeCode.
	   * @return List<Place>
	   */
	public static List<Place> getListPlace() {

	    Cursor cursor = DatabaseManager.getDatabase()
	        .query("Place", null, null, null, null, null, "placeCode");
	    
	    List<Place> list = new ArrayList<Place>();

	    while (cursor.moveToNext()) {
	      list.add(convertCursorToPlace(cursor));
	    }
	    cursor.close();
	    return list;
	}
	
	/**
	   * gets the Place by code.
	   * 
	   * @param placeCode placeCode.
	   * @return List<Place>
	   */
	public static List<Place> getPlaceByCode(String placeCode) {
		String[] values = { placeCode };
		
	    Cursor cursor = DatabaseManager.getDatabase()
	        .query("Place", null, " placeCode = ? ", values, null, null, "placeCode");
	    
	    
	    List<Place> list = new ArrayList<Place>();

	    while (cursor.moveToNext()) {
	      list.add(convertCursorToPlace(cursor));
	    }
	    cursor.close();
	    return list;
	}
	
	/**
	   * convert a cursor into a Place object.
	   * 
	   * @param cursor cursor.
	   * @return Place
	   */
	  private static Place convertCursorToPlace(Cursor cursor) {

	    Place place = new Place(
	    		cursor.getString(0), 
	    		cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4),
	    		cursor.getInt(5), cursor.getString(6), cursor.getDouble(7), cursor.getDouble(8));
	    
	    return place;
	  }

	/*
	 * (non-Javadoc)
	 * 
	 * @see co.com.umobile.dao.GenericDao#insertOrUpdateData(java.lang.String, int)
	 */
	public void insertOrUpdateData(String values, int serviceNumber) {
		String sqlText = "insert or replace into Place(placeCode, placeName, placeDescription, room, block, placeType, placeTypeDescription, longitude, latitude) values(" + values + ")";
	    DatabaseManager.getDatabase().execSQL(sqlText);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see co.com.umobile.dao.GenericDao#deleteDataMirror()
	 */
	public void deleteDataMirror() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see co.com.umobile.dao.GenericDao#copyDataToMirror()
	 */
	public void copyDataToMirror() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see co.com.umobile.dao.GenericDao#deleteDataOriginal()
	 */
	public void deleteDataOriginal() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see co.com.umobile.dao.GenericDao#copyDataToOriginal()
	 */
	public void copyDataToOriginal() {
		// TODO Auto-generated method stub

	}

}
